/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.toolbar;

import org.rails.extjs.Option;
import org.rails.extjs.data.Store;
import org.rails.extjs.javascript.JavaScript;

public class PagingToolbar extends Toolbar {
	
	public PagingToolbar() {
		super();
		setDisplayInfo(true);
	}

	public PagingToolbar(Store store,int pageSize) {
		super();
		setStore(store);
		setPageSize(pageSize);
		setDisplayInfo(true);
	}
	
	public PagingToolbar(String storeId,int pageSize) {
		super();
		setStore(storeId);
		setPageSize(pageSize);
		setDisplayInfo(true);
	}
	
	public PagingToolbar(String id,Store store,int pageSize) {
		super(id);
		setStore(store);
		setPageSize(pageSize);
		setDisplayInfo(true);
	}
	
	public PagingToolbar(Option configOptions) {
		super(configOptions);
	}
	
	public void setAfterPageText(String afterPageText){
		addOption("afterPageText", afterPageText);
	}
	
	public String getAfterPageText(){
		return getString("afterPageText", "of {0}");
	}
	
	public void setBeforePageText(String beforePageText){
		addOption("beforePageText", beforePageText);
	}
	
	public String getBeforePageText(){
		return getString("beforePageText", "Page");
	}

	public void setDisplayInfo(Boolean displayInfo){
		addOption("displayInfo", displayInfo);
	}
	
	public Boolean isDisplayInfo(){
		return getBoolean("displayInfo",false);
	}
	
	public void setDisplayMsg(String displayMsg){
		addOption("displayMsg", displayMsg);
	}
	
	public String getDisplayMsg(){
		return getString("displayMsg", "Displaying {0} - {1} of {2}");
	}
	
	public void setEmptyMsg(String emptyMsg){
		addOption("emptyMsg",emptyMsg);
	}
	
	public String getEmptyMsg(){
		return getString("emptyMsg","No data to display");
	}
	
	public void setFirstText(String firstText){
		addOption("firstText", firstText);
	}

	public String getFirstText(){
		return getString("firstText","First Page");
	}
	
	public void setLastText(String lastText){
		addOption("lastText", lastText);
	}
	
	public String getLastText(){
		return getString("lastText", "Last Page");
	}
	
	public void setNextText(String nextText){
		addOption("nextText", nextText);
	}

	public String getNextText(){
		return getString("nextText", "Next Page");
	}
	
	public void setPageSize(Integer pageSize){
		addOption("pageSize", pageSize);
	}

	public Integer getPageSize(){
		return getInteger("pageSize", 20);
	}
	
	public void setPrependButtons(Boolean prependButtons){
		addOption("prependButtons", prependButtons);
	}
	
	public Boolean isPrependButtons(){
		return getBoolean("prependButtons", false);
	}
	
	public void setPrevText(String prevText){
		addOption("prevText", prevText);
	}
	
	public String getPrevText(){
		return getString("prevText", "Previous Page");
	}
	
	public void setRefreshText(String refreshText){
		addOption("refreshText", refreshText);
	}
	
	public String getRefreshText(){
		return getString("refreshText", "Refresh");
	}
	
	public void setStore(Store store){
		addOption("store",store);
	}
	
	public void setStore(String storeId){
		addOption("store", new JavaScript("Ext.StoreMgr.lookup('{0}')",storeId));
	}
	
	public Store getStore(){
		if(getOption("store") instanceof Store)
			return (Store) getOption("store");
		else
			return null;
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.PagingToolbar(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}
	
}
